package com.catchdays;

import java.sql.SQLException;
import java.util.Calendar;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.catchdays.db.DatabaseHelper;
import com.catchdays.db.model.CatchDaysModel;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EActivity;
import com.googlecode.androidannotations.annotations.OptionsItem;
import com.googlecode.androidannotations.annotations.OptionsMenu;
import com.googlecode.androidannotations.annotations.OrmLiteDao;
import com.googlecode.androidannotations.annotations.ViewById;
import com.j256.ormlite.dao.Dao;

@EActivity(R.layout.activity_add)
@OptionsMenu(R.menu.add)
public class AddActivity extends SherlockActivity implements OnItemSelectedListener {
	private static final String TAG = "AddActivity";

	Calendar cal = Calendar.getInstance();

	@OrmLiteDao(helper = DatabaseHelper.class, model = CatchDaysModel.class)
	Dao<CatchDaysModel, Integer> catchDaysDao;

	@ViewById
	Button datePickerButton;

	@ViewById
	Spinner anniversaryType;

	private int id;
	private CatchDaysModel catchDays;
	private int year;
	private int month;
	private int day;

	@AfterViews
	public void init() {
		Intent intent = getIntent();
		id = intent.getIntExtra("id", 0);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);

		if (id == 0) {
			getSupportActionBar().setTitle(R.string.menu_add);
		} else {
			getSupportActionBar().setTitle(R.string.menu_edit);

			try {
				catchDays = catchDaysDao.queryForId(id);
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}

		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.listTypes, R.layout.sherlock_spinner_item);
		adapter.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

		anniversaryType.setPrompt("골라봐");
		anniversaryType.setAdapter(adapter);
		anniversaryType.setOnItemSelectedListener(this);
	}

	@Click
	public void datePickerButton() {
		Calendar cal = Calendar.getInstance();

		new DatePickerDialog(this, datePickerListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
	}

	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

		@Override
		public void onDateSet(DatePicker view, int yearr, int monthOfYear, int dayOfMonth) {
			year = yearr;
			month = monthOfYear;
			day = dayOfMonth;
		}
	};

	@OptionsItem
	public void menuSave() {
		Toast.makeText(this, "저장을 클릭하였습니다", Toast.LENGTH_SHORT).show();
	}

	@OptionsItem
	public void homeSelected() {
		finish();
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}
}
